﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Planer.Model;

namespace Planer.UI.Controls
{
    public partial class TransactionControl : UserControl
    {
        action _action;
        periodical_action_transanction _transactionP;
        allocation_transanction _transactionA;

        public object Transaction
        {
            get
            {
                if (_action != null) return _action;
                if (_transactionA != null) return _transactionA;
                return _transactionP;
            }
            set
            {
                if (value == null) return;
                if (value is action)
                {
                    _action = (action)value;
                }
                else if(value is  periodical_action_transanction)
                {
                    _transactionP = (periodical_action_transanction)value;
                }
                else if (value is allocation_transanction)
                {
                    _transactionA = (allocation_transanction)value;
                }
                CustomInitializeComponent();
            }
        }

        public TransactionControl()
        {
            InitializeComponent();
        }

        public void CustomInitializeComponent()
        {
            if (_action != null)
            {
                labelSumm.Text = String.Format("{0:C}", _action.amount);
                labelCategory.Text = _action.action_category.name;
                labelDescr.Text = _action.descr;
                labelDate.Text = _action.create_date.ToShortDateString();
            }
            else if(_transactionP != null)
            {
                labelSumm.Text = String.Format("{0:C}", _transactionP.periodical_action.amount);
                labelCategory.Text = _transactionP.periodical_action.action_category.name;
                labelDescr.Text = _transactionP.periodical_action.descr;
                labelDate.Text = _transactionP.create_date.ToShortDateString();
            }
            else if (_transactionA != null)
            {
                labelSumm.Text = String.Format("{0:C}", -_transactionA.allocation.amount);
                if (_transactionA.allocation.financial_goal != null)
                {
                    labelCategory.Text = _transactionA.allocation.financial_goal.name;
                    labelDescr.Text = _transactionA.allocation.financial_goal.descr;
                }
                else
                {
                    labelCategory.Text = _transactionA.allocation.accumulation.name;
                    labelDescr.Text = _transactionA.allocation.accumulation.descr;
                }
                labelDate.Text = _transactionA.create_date.ToShortDateString();
            }
            else { return; }            
        }
    }
}
